package com.devops.admin.graphql;

import com.devops.admin.model.DOSMenu;
import com.devops.admin.po.DOSMenuPo;
import com.devops.admin.po.DOSRoleMenuRelationPo;
import com.devops.admin.service.DOSMenuService;
import com.devops.admin.vo.DOSMenuVo;
import com.devops.common.annotation.DOSLog;
import com.devops.common.utils.Message;
import com.devops.constant.DevOpsConstant;
import com.devops.shiro.vm.BaseUserVM;
import com.netflix.graphql.dgs.*;
import graphql.execution.DataFetcherResult;
import graphql.schema.DataFetchingEnvironment;
import io.swagger.models.auth.In;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

/**
 * @ClassName DOSMenuDataFetcher
 * @Description TODO
 * @Author heyabo
 * @Date 2022/8/4 09:30
 * @Version 1.0
 **/
@DgsComponent
@RequiresPermissions("loginUser")
public class DOSMenuDataFetcher {

    @Autowired
    DOSMenuService menuService;

    /**
     * @Author heyabo
     * @Description 保存菜单
     * @Date 16:46 2022/8/30
     * @Param [menuVo]
     * @return com.devops.common.utils.Message
     **/
    @DgsMutation
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR},logical = Logical.OR)
    @DOSLog(description = "保存菜单")
    public Message saveMenu(@InputArgument DOSMenuVo menuVo){

        return menuService.saveMenu(menuVo);
    }

    @DgsMutation
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR},logical = Logical.OR)
    @DOSLog(description = "新建菜单")
    public Message addNewMenu(@InputArgument DOSMenuVo menuVo){

        return menuService.saveMenu(menuVo);
    }

    /**
     * @Author heyabo
     * @Description 修改菜单
     * @Date 17:00 2022/8/30
     * @Param [menuVo]
     * @return com.devops.common.utils.Message
     **/
    @DgsMutation
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR},logical = Logical.OR)
    @DOSLog(description = "修改菜单")
    public Message updateMenu(@InputArgument List<DOSMenuVo> menuVos){

        return menuService.updateMenu(menuVos);
    }

    /**
     * @Author heyabo
     * @Description 修改单个菜单
     * @Date 17:00 2022/8/30
     * @Param [menuVo]
     * @return com.devops.common.utils.Message
     **/
    @DgsMutation
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR},logical = Logical.OR)
    @DOSLog(description = "修改单个菜单")
    public Message updateSingleMenu(@InputArgument DOSMenuVo menuVo){

        return menuService.updateSingleMenu(menuVo);
    }

    /**
     * @Author heyabo
     * @Description 删除菜单
     * @Date 11:39 2022/9/2
     * @Param [menuIds]
     * @return com.devops.common.utils.Message
     **/
    @DgsMutation
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR},logical = Logical.OR)
    @DOSLog(description = "删除菜单")
    public Message deleteMenu(@InputArgument Integer menuId){

        return menuService.deleteMenu(menuId);
    }

    @DgsQuery
    public List<DOSMenuPo> getMenus(){
        return menuService.getMenus();
    }

    @DgsQuery
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.LOGIN_USER},logical = Logical.OR)
    public DataFetcherResult<List<DOSMenu>> getDOSMenus() {
        Map<String,Object> result = menuService.dealAllMenus();
        List<DOSMenu> firstMenus = (List<DOSMenu>) result.get("firstMenus");
        Map<Integer,List<DOSMenu>> dealMenus = (Map<Integer,List<DOSMenu>>) result.get("dealMenus");
        return DataFetcherResult.<List<DOSMenu>>newResult()
                .data(firstMenus)
                .localContext(dealMenus)
                .build();
    }

    @DgsData(parentType = "DOSMenu",field = "subMenus")
    public List<DOSMenu> getDOSSubMenus(DgsDataFetchingEnvironment dfe) {
        DOSMenu dosMenu = dfe.getSource();
        Map<Integer,List<DOSMenu>> dealMenus = dfe.getLocalContext();
        return  dealMenus.get(dosMenu.getId());
    }

    /**
     * @Author heyabo
     * @Description 获取角色绑定和未绑定的菜单
     * @Date 10:13 2022/8/30
     * @Param [roleId]
     * @return java.util.List<com.devops.admin.po.DOSRoleMenuRelationPo>
     **/
    @DgsQuery
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR},logical = Logical.OR)
    @DOSLog(description = "获取角色绑定和未绑定的菜单")
    public List<DOSRoleMenuRelationPo> getBindMenus(Integer roleId,Integer isDel){

        return menuService.getBindMenus(roleId,isDel);
    }

    /**
     * @Author heyabo
     * @Description 获取未添加的菜单
     * @Date 15:22 2022/8/30
     * @Param []
     * @return java.util.List<com.devops.admin.model.DOSMenu>
     **/
    @DgsQuery
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR},logical = Logical.OR)
    @DOSLog(description = "获取未添加的菜单")
    public List<DOSMenu> getNoAddMenus(){

        return menuService.getNoAddMenus();
    }
}
